<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
	 <meta charset="utf-8">
	 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	 <title th:replace="common/base::pagetitle"></title>
	 <link th:replace="common/base::static"/>
	 <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/material/easyui.css}">
	 <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/icon.css}">
	 <link rel="stylesheet" th:href="@{/static/css/wlb-treegrid.css}">
</head>
<body style="background-color: #f2f2f2;">

<div id="kPage" class="layui-fluid" style="padding: 0">
	 <div class="layui-row">
		  <!-- 主要内容块 -->
		  <div class="layui-col-md12" id="kMainContentCol">
				<div class="layui-fluid wlb-fluid-padding15">
					 <div class="layui-card">
						  <form>
								<div class="layui-form layui-card-header wlb-card-header-auto">
									 <div class="layui-form-item">
										  <div class="layui-inline">
												<label class="layui-form-label">名称</label>
												<div class="layui-input-inline">
													 <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
												</div>
										  </div>
										  <div class="layui-inline">
												<button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">
													 <i class="layui-icon layui-icon-search"></i>
												</button>
												<button class="layui-btn " type="reset">
													 <i class="layui-icon layui-icon-refresh-1"></i>
												</button>
										  </div>
									 </div>
								</div>
						  </form>

						  <div class="layui-card-body">
								<table id="kContentTable" lay-filter="kContentTable"></table>
						  </div>
					 </div>

				</div>
		  </div>
	 </div>
</div>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>
<script th:src="@{/static/plugins/easyui/easyloader.js}"></script>

<script type="text/html" id="statusTpl">
	 {{# if(d.status === 0) { }}
	 <span class="layui-badge layui-bg-green">正常</span>
	 {{# } else { }}
	 <span class="layui-badge layui-bg-gray">禁用</span>
	 {{# } }}
</script>

<script type="text/html" id="typeTpl">
	 {{#  if(d.type === 0){ }}
	 <span class="layui-badge layui-bg-blue">供应商</span>
	 {{#  } else if(d.type === 1) { }}
	 <span class="layui-badge layui-bg-orange">核心企业</span>
	 {{#  } else if(d.type === 2) { }}
	 <span class="layui-badge layui-bg-green">银行</span>
	 {{#  } else if(d.type === 9) { }}
	 <span class="layui-badge layui-bg-green">平台</span>
	 {{#  } else { }}
	 <span class="layui-badge layui-bg-cyan">其他</span>
	 {{# } }}
</script>

<script type="text/html" id="sourceTpl">
	{{#  if(d.source == 0){ }}
	<span class="layui-badge layui-bg-blue">集采推送</span>
	{{#  } else if(d.source == 1) { }}
	<span class="layui-badge layui-bg-orange">其他</span>
	{{# } }}
</script>



<!--头部工具栏模板-->
<script type="text/html" id="headToolbarTpl">
	<button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
	<button class="layui-btn layui-btn-sm" lay-event="batchdel">删除</button>
</script>


<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
<!--&lt;!&ndash;	 {{# if(d.type == 1) { }}&ndash;&gt;-->
<!--	 <a class="layui-btn layui-btn-xs" data-event="add" onclick="active.add(this)" data-comId="{{d.comId}}"><i class="layui-icon layui-icon-add-1"></i></a>-->
<!--&lt;!&ndash;	 {{# } }}&ndash;&gt;-->
	 <a class="layui-btn layui-btn-normal layui-btn-xs" data-event="edit" onclick="active.edit(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
	 <a class="layui-btn layui-btn-danger layui-btn-xs" data-event="del" onclick="active.del(this)" data-id="{{d.id}}" data-orgId="{{d.orgId}}"><i class="layui-icon layui-icon-delete"></i>删除</a>
<!--{{# if(d.type != 0){ }}-->
<!--	<a class="layui-btn layui-bg-orange layui-btn-xs" data-event="queryOrgRelations" onclick="active.queryRelation(this)" data-id="{{d.id}}" data-type="{{d.type}}"><i class="layui-icon layui-icon-cols"></i>查询关联关系</a>-->
<!--{{# } }}-->
<!--	<a class="layui-btn layui-bg-black layui-btn-xs" data-event="queryBank" onclick="active.queryBankList(this)" data-comId="{{d.comId}}" data-orgId="{{d.orgId}}"><i class="layui-icon layui-icon-cols"></i>查询绑定银行列表</a>-->
	 <!--创建签名账号-->
<!--	 {{# if(d.accountId == null || d.accountId.length == 0){ }}-->
<!--	 	<a class="layui-btn layui-btn-warm layui-btn-xs"  data-event="sign" onclick="active.sign(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-friends"></i>签名账号</a>-->
<!--	 {{# } }}-->
</script>

<script type="text/javascript" th:inline="none">
    var active;
    layui.use(['element', 'form', 'layer', 'table', 'laytpl'], function () {
		var elemet = layui.element, form = layui.form, layer = layui.layer, table = layui.table;
		var tableId = 'kContentTable';

		// 实例化table
		var tableIns = table.render($.extend(config.layui.table, {
			elem: '#' + tableId
			, url: api.sys.deptListData //数据接口
			, toolbar: '#headToolbarTpl'
			, cols: [[ //表头
				{type: 'checkbox', fixed: 'left'}
				, {field: 'name', title: '机构名称', width: 120, fixed: false}
            // , {field: 'accountId', title: '签名账号',  width: 180}
				, {field: 'type', title: '类型', width: 100, align: 'center', templet: '#typeTpl', fixed: false}
				, {field: 'comAddress', title: '公司地址',  width: 220, fixed: false}
				, {field: 'comCerNo', title: '工商注册号',  width: 220, fixed: false}
				, {field: 'source', title: '机构来源',  width: 100, templet: '#sourceTpl', fixed: false}
				, {field: '', title: '操作', width: 370, fixed: 'right', templet: '#rowToolbarTpl'}
			]]
		}));
		var area = ['450px', '530px'];

		// 监听表格头部事件
		table.on('toolbar(' + tableId + ')', function (obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			var checkData = checkStatus.data; // 得到选中的数据
			switch (obj.event) {
				case 'add':
					var id = 0;
					id = id || 0;
					var level = 0

					layer.open({
						type: 2
						, title: '添加'
						, content: api.sys.deptEditView + '?parentId=' + id + '&type=' + level
						, maxmin: true
						, area: area   // 宽高
						, btn: ['确定', '取消']
						, yes: function (index, layero) {
							//点击确认触发 iframe 内容中的按钮提交
							var submit = layero.find('iframe').contents().find("#kFormSubmit");
							submit.click();
						}
					});
					break;
				case 'batchdel':
					if (checkData.length === 0) {
						return wlbKit.warningMsg('请选择数据');
					}
					layer.confirm('确定删除吗？', function (index) {

						var ids = [];
						$.each(checkData, function (i, n) {
							ids[i] = n.id;
						});

						//执行 Ajax 后重载
						$.ajax({
							type: req.type.post,
							url: api.sys.deptRm,
							data: {ids: ids.toString()},
							success: function (r) {
								if (r.code === req.status.ok) {
									table.reload(tableId);
									wlbKit.sucessMsg('已删除');
								} else {
									wlbKit.errorMsg(r.msg);
								}
							}
						});

					});
					break;
			}
		});

        // easyloader.load('treegrid', function () {
		//
        //     treeGridInst = $('#' + treeGridId).treegrid($.extend(config.easyui.treegrid, {
        //         url: api.sys.deptListData,
        //         method: 'get',
        //         idField: 'id',
        //         treeField: 'name',
        //         toolbar: '#headToolbarTpl',
        //         onExpand: function (row) {
        //             treeGridKit.fitRightFrozenCol(treeGridInst);
        //         },
        //         onCollapse: function (row) {
        //             treeGridKit.fitRightFrozenCol(treeGridInst);
        //         },
        //         loadFilter: function (r) {
        //             var rows = r.records;
        //             r.rows = rows;
        //             return r;
        //         },
        //         onLoadSuccess: function (row, data) {
        //             treeGridKit.fitRightFrozenCol(treeGridInst);
        //         },
        //         frozenColumns: [[
		// 			{field: 'toolbar', title: '操作', width: 200, align: 'center', fixed: true, formatter: formatter.rowToolbar}
        //         ]],
        //         columns: [[
		// 			{field: 'compName', title: '机构名称', width: 220, fixed: false}
        //             , {field: 'compType', title: '类型', width: 100, align: 'center', formatter: formatter.type, fixed: true}
        //             , {field: 'comAddress', title: '公司地址',  width: 220, fixed: false}
        //             , {field: 'source', title: '机构来源',  width: 220, formatter: formatter.source, fixed: true}
        //             // , {field: 'status', title: '状态', width: 100, align: 'center', formatter: formatter.status, fixed: true}
        //             // , {field: 'sort', title: '排序号', width: 100, align: 'center', fixed: true}
        //             // , {field: 'createTime', title: '创建时间', width: 180, fixed: true}
        //         ]]
		//
        //     }));
		//
        //     treeGridKit.setPager(treeGridInst);
        // });

        // 监听事件
        active = {
            edit: function (that) {
                var id = $(that).data('id');
                layer.open({
                    type: 2
                    , title: '编辑'
                    , content: api.sys.deptEditView + '?id=' + id
                    , maxmin: true
                    , area: area   // 宽高
					, btn: ['确定', '取消']
					, yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#kFormSubmit");
                        submit.click();
                    }
                });
            },
            sign: function (that) {
                var id = $(that).data('id');
                layer.open({
                    type: 2
                    , title: '创建签名账号'
                    , content: BASE_WEB + 'esign/createOrgView?id=' + id
                    , maxmin: true
                    , area: area   // 宽高
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#kFormSubmit");
                        submit.click();
                        table.reload(tableId);
                    }
                });
            },
            del: function (that) {
               layer.confirm('确定删除吗？', function (index) {
                    var id = $(that).data('id');

                    // 执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: api.sys.deptRm,
						data: {ids: id.toString()},
                        async: false,
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                // treeGridKit.reload(treeGridInst);
								table.reload(tableId);
                                wlbKit.sucessMsg('已删除');
                            } else {
                                wlbKit.errorMsg(r.msg);
                            }
                        }
                    });
                });

            },
			queryRelation:function (that) {
				var id = $(that).data('id');
				var comType = $(that).data('type');
				var area2 = ['80%', '80%'];
				var index = layer.open({
					type: 2
					, title: '关联列表'
					, content: api.sys.deptRelationView + '?comType=' + Number(comType) + '&id=' + id
					, maxmin: true
					, area: area2   // 宽高
					// , btn: ['确定', '取消']
					// , yes: function (index, layero) {
					// 	//点击确认触发 iframe 内容中的按钮提交
					// 	debugger
					// 	var submit = layero.find('iframe').contents().find("#kTableSubmit");
					// 	submit.click();
					// }
				});
				// layer.full(index);
			},
			queryBankList:function (that) {
            	// 机构id
				var compId = $(that).data('compid');
				var index = layer.open({
					type: 2
					, title: '关联银行列表'
					, content: api.sys.deptBankView + '?compId=' + compId
					, maxmin: true
					, area: area   // 宽高
					, btn: ['确定', '取消']
					, yes: function (index, layero) {
						//点击确认触发 iframe 内容中的按钮提交
						var row = layero.find('iframe').contents().find("#kFormSubmit");
						layer.alert(row[0].value);
						submit.click();
					}
				});
				layer.full(index);
			}
        };

        wlbKit.activeInit(active);

        $(window).resize(function () {
            treeGridKit.fitRightFrozenCol(tableId);
        });

        // 列格式化函数
        var formatter = {
            rowToolbar: function (value, row, index) {
                return wlbKit.renderTpl('rowToolbarTpl', row);
            },
            status: function (value, row, index) {
                return wlbKit.renderTpl('statusTpl', row);
            },
            type: function (value, row, index) {
                return wlbKit.renderTpl('typeTpl', row);
            },
			source: function (value, row, index) {
				return wlbKit.renderTpl('sourceTpl', row);
			}
        };

        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            var params = obj.field;
            log('seach=', params); //

			tableIns.reload({
				where: params,
				page: {curr: 1}
			});
			return false;

        });

        // 绑定监听回车事件
        wlbKit.bindEnterEventForLayuiForm();

    });
</script>

</body>
</html>
